package com.bank.controller;

import com.bank.constant.MessageConstant;
import com.bank.dto.LoanDTO;
import com.bank.dto.RepayDTO;
import com.bank.entity.Loan;
import com.bank.entity.LoanTransaction;
import com.bank.result.PageResult;
import com.bank.result.Result;
import com.bank.service.LoanService;
import com.bank.vo.LoanTransactionVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/loan")
@Slf4j
@Api(tags = "5.借贷")
public class LoanController {
    @Autowired
    private LoanService loanService;

    @PostMapping("/")
    @ApiOperation("借款")
    @Transactional
    public Result<String> getLoan(@RequestBody @Valid LoanDTO loanDTO) {
        log.info("借款请求参数：{}", loanDTO);
        loanService.getLoan(loanDTO);
        return Result.success(MessageConstant.LOAN_SUCCESS);
    }

    @GetMapping("/{id}")
    @ApiOperation("查询借款信息（不包括已还款信息）")
    public Result<List<LoanTransactionVO>> getAllLoanTransaction(@PathVariable("id") Long accountId) {
        log.info("查询借款信息请求参数：{}", accountId);
        List<LoanTransactionVO> loanTransactionVOList = loanService.getAllLoanTransaction(accountId);
        return Result.success(loanTransactionVOList, MessageConstant.TRANSACTION_QUERY_SUCCESS);
    }

    @PostMapping("/repay")
    @ApiOperation("还款")
    @Transactional
    public Result<String> repayLoan(@RequestBody @Valid RepayDTO repayDTO) {
        log.info("还款请求参数：{}", repayDTO);
        loanService.repayLoan(repayDTO);
        return Result.success(MessageConstant.REPAY_SUCCESS);
    }
}
